package cn.mumzone.pos.ui.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.support.annotation.NonNull;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.mumzone.basic.util.DisplayUtil;
import cn.mumzone.basic.util.ListUtil;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.basic.util.log.Elog;
import cn.mumzone.pos.R;
import cn.mumzone.pos.model.MemberEntity;
import cn.mumzone.pos.model.ServiceEntity;
import cn.mumzone.pos.util.PopupUtil;

/**
 * Created by qiuzhenhuan on 2019/6/14.
 */

public class ADA_CartServices extends RecyclerView.Adapter<ADA_CartServices.ViewHolder> {

    private Context mContext;

    private List<ServiceEntity> mData;

    private MemberEntity memberEntity;

    private ViewHolder mPopupAnchorHolder;
    private PopupWindow mPopHint;
    private boolean isPopupShowing;

    public ADA_CartServices(Context context) {
        this.mContext = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_cart_service, parent, false);
        return new ViewHolder(view);
    }


    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
        ServiceEntity entity = mData.get(position);
        holder.tvCartServiceName.setText(StringUtils.getString(!TextUtils.isEmpty(entity.getServiceName()) ?
                entity.getServiceName() : entity.getName()));
        holder.tvServiceInfo.setText(String.format("%s (%s，%s分钟)", StringUtils.getString(!TextUtils.isEmpty(entity.getTechnicianName()) ?
                        entity.getTechnicianName() : entity.getPerson()),
                StringUtils.isEmpty(entity.getTechnicianLabel()) ? "服务技师" : entity.getTechnicianLabel(),
                StringUtils.getString(entity.getServiceTime())));

        if (memberEntity != null && MemberEntity.DISCOUNT_TYPE_MEMBER_PRICE.equals(memberEntity.getDiscountType())
                && !StringUtils.isEmpty(entity.getMemberPrice())) {
            holder.tvCartOriginPrice.setVisibility(View.VISIBLE);
            holder.tvCartOriginPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
            holder.tvCartOriginPrice.setText(StringUtils.getFormattedMoneyWithUnit(entity.getOriginPrice()));
            holder.tvCartServicePrice.setText(StringUtils.getFormattedMoneyWithUnit(entity.getMemberPrice()));
        } else {
            holder.tvCartOriginPrice.setVisibility(View.GONE);
            holder.tvCartServicePrice.setText(StringUtils.getFormattedMoneyWithUnit(entity.getOriginPrice()));
        }

        holder.ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onItemDeleteListener != null) {
                    onItemDeleteListener.onItemDeleted(position);
                }
            }
        });

        if (position == 0) {
            mPopupAnchorHolder = holder;
            showPopupHint(holder.ivGoRight);
        }

    }

    public int getAnchorViewPosition() {
        if (mPopupAnchorHolder != null) {
            mPopupAnchorHolder.ivGoRight.measure(0, 0);
            int[] location = new int[2];
            mPopupAnchorHolder.ivGoRight.getLocationOnScreen(location);
            return location[1] + mPopupAnchorHolder.ivGoRight.getMeasuredHeight() * 2;
        }

        return -1;
    }

    public void dismissPopup() {
        if (mPopHint != null) {
            mPopHint.dismiss();
            mPopHint = null;
            isPopupShowing = false;
        }
    }

    public boolean isPopupShowing() {
        return isPopupShowing;
    }

    public interface OnItemDeleteListener {
        void onItemDeleted(int position);
    }

    public void setBindMember(MemberEntity bindMember) {
        memberEntity = bindMember;
    }

    private OnItemDeleteListener onItemDeleteListener;

    public void setOnItemDeleteListener(OnItemDeleteListener onItemDeleteListener) {
        this.onItemDeleteListener = onItemDeleteListener;
    }

    public List<ServiceEntity> getData() {
        return mData;
    }

    public void setData(List<ServiceEntity> data) {
        this.mData = data;
    }

    @Override
    public int getItemCount() {
        return ListUtil.isListEmpty(mData) ? 0 : mData.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_cart_service_name)
        TextView tvCartServiceName;
        @BindView(R.id.tv_service_info)
        TextView tvServiceInfo;
        @BindView(R.id.tv_cart_service_price)
        TextView tvCartServicePrice;
        @BindView(R.id.tv_cart_origin_price)
        TextView tvCartOriginPrice;
        @BindView(R.id.iv_delete)
        AppCompatImageView ivDelete;
        @BindView(R.id.iv_go_right)
        AppCompatImageView ivGoRight;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public void showPopupHint(final View targetView) {
        if (!isPopupShowing) {
            isPopupShowing = true;
            targetView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mPopHint = PopupUtil.showBlackBgPopup(targetView,
                            PopupUtil.OPEN_ORDER_MODIFY_SERVER,
                            "点击修改服务技师", PopupUtil.LEFT_BOTTOM);
                }
            }, 200);
        }
    }
}
